﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class ManualTool_WeiXiu_Insert : Form
    {
        string 手工具編號, 手工具名稱, 規格型號, 廠牌代號, 存放地點代號, 數量, str;
        bool b手工具Search, b手工具Insert, b手工具Edit, b手工具Del;
        static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        public ManualTool_WeiXiu_Insert(bool b手工具Search, bool b手工具Insert, bool b手工具Edit, bool b手工具Del, string str, string 手工具編號, string 手工具名稱, string 規格型號, string 廠牌代號, string 存放地點代號, string 數量)
        {
            InitializeComponent();
            this.手工具編號 = 手工具編號;
            this.手工具名稱 = 手工具名稱;
            this.廠牌代號 = 廠牌代號;
            this.存放地點代號 = 存放地點代號;
            this.規格型號 = 規格型號;
            this.數量 = 數量;
            this.str = str;
            this.b手工具Del = b手工具Del;
            this.b手工具Edit = b手工具Edit;
            this.b手工具Insert = b手工具Insert;
            this.b手工具Search = b手工具Search;
        }

        private void ManualTool_WeiXiu_Insert_Load(object sender, EventArgs e)
        {
            if (str.Equals("維修"))
            {
                lb_日期.Text = "維修日期";
                lb_數量.Text = "維修數量";
            }
            if (str.Equals("送檢"))
            {
                this.Text = "手工具送檢";
                lb_日期.Text = "送檢日期";
                lb_數量.Text = "送檢數量";
            }
            if (str.Equals("報廢"))
            {
                this.Text = "手工具報廢";
                lb_日期.Text = "報廢日期";
                lb_數量.Text = "報廢數量";
            }
            txt_手工具編號.Text = 手工具編號;
            txt_規格型號.Text = 規格型號;
            txt_存放地點.Text = 存放地點代號;
            txt_廠牌.Text = 廠牌代號;
            txt_手工具名稱.Text = 手工具名稱;
        }

        private void bt_受理人_search_Click(object sender, EventArgs e)
        {
            VehicleSearch frm = new VehicleSearch(txt_受理人);
            frm.ShowDialog();
        }

        private void bt_存檔_Click(object sender, EventArgs e)
        {
            if (str.Equals("維修"))
            {
                save("sp_手工其維修_insert");
            }
            if (str.Equals("送檢"))
            {
                save("sp_手工具送檢_insert");
            }
            if (str.Equals("報廢"))
            {
                save("sp_手工具報廢_insert");
            }
        }

        public void save(string procedure)
        {
            try
            {
                if (txt_手工具編號.Text.ToString() == null || txt_手工具編號.Text.ToString().Equals(""))
                    MessageBox.Show("領用人代號還沒輸入資料");
                else
                {
                    DateTime dt1, dt2;
                    dt1 = dateTime_領用日期.Value;
                    dt2 = dateTime_歸還日期.Value;
                    if (DateTime.Compare(dt1, dt2) > 0)
                    {
                        MessageBox.Show("輸入日期不對！");
                    }
                    else
                    {
                        int check1 = Int32.Parse(數量);
                        int check2 = Int32.Parse(txt_領用數量.Text.ToString());
                        if (check1 < check2 || check2 <= 0)
                        {
                            MessageBox.Show("0 < 領用數量（<=）" + check1);
                        }
                        else
                        {
                            SqlConnection con = new SqlConnection(connectionString);
                            con.Open();
                            SqlCommand cmd0;
                            cmd0 = new SqlCommand(procedure, con);
                            cmd0.CommandType = CommandType.StoredProcedure;
                            cmd0.Parameters.AddWithValue("@手工具編號", txt_手工具編號.Text.ToString());
                            cmd0.Parameters.AddWithValue("@受理人代號", txt_受理人.Text.ToString());
                            cmd0.Parameters.AddWithValue("@數量", Int32.Parse(txt_領用數量.Text.ToString()));
                            cmd0.Parameters.AddWithValue("@備註", txt_備註.Text.ToString());
                            cmd0.Parameters.AddWithValue("@維修日期", dateTime_領用日期.Value);
                            cmd0.Parameters.AddWithValue("@歸還日期", dateTime_歸還日期.Value);
                            cmd0.ExecuteNonQuery();
                            //
                            cmd0 = new SqlCommand("sp_手工具_手工具領用_insert", con);
                            cmd0.CommandType = CommandType.StoredProcedure;
                            cmd0.Parameters.AddWithValue("@手工具編號", txt_手工具編號.Text.ToString());
                            cmd0.Parameters.AddWithValue("@數量", Int32.Parse(txt_領用數量.Text.ToString()));
                            cmd0.ExecuteNonQuery();
                            MessageBox.Show("維修成功");
                            con.Close();
                            //***********
                            this.Close();
                            ManualTool_Weixiu frm = new ManualTool_Weixiu(str, b手工具Search, b手工具Insert, b手工具Edit, b手工具Del);
                            frm.ShowDialog();                          
                        }
                    }
                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        private void bt_離開_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txt_受理人_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 手工具受理人 where 受理人代號=N'{0}'", txt_受理人.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lb_受理人.Text = "";
                else lb_受理人.Text = temp;
                con.Close();
            }
            catch (Exception ex) { }
        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void txt_領用數量_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) || e.KeyChar == '.')
            {
                e.Handled = true;
                MessageBox.Show("很抱歉，請您輸入號碼", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
